<?php

namespace app\modules\Supplier\models;

use Yii;
use app\modules\Basic\models\HotelBrand;
use app\modules\Basic\models\CityCode;
use app\modules\Manage\models\User;
/**
 * This is the model class for table "supplier".
 *
 * @property int $id
 * @property int $type 供应商类型
 * @property string $en_name 英文名称
 * @property int $hotel_group 酒店集团
 * @property int $is_tour 是否导游
 * @property string $phone 导游联系电话
 * @property string $city 城市
 * @property string $remark 备注
 * @property int $status 1正常  2禁用
 * @property int $user_id 创建人ID
 * @property string $created_at 创建时间
 * @property string $updated_at
 */
class Supplier extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'supplier';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['type', 'hotel_group', 'is_tour', 'status', 'user_id','nation'], 'integer'],
            [['en_name','city','phone'], 'required'],
            [['created_at', 'updated_at'], 'safe'],
            [['en_name','address','email','url'], 'string', 'max' => 255],
            [['phone'], 'string', 'max' => 32],
            [['person','city'], 'string', 'max' => 64],
            [['remark'], 'string', 'max' => 512],
            ['person','validatePerson','skipOnEmpty' => false, 'skipOnError' => false],
        ];
    }


    public function validatePerson(){
        if($this->type != 5){
            if(empty($this->person)){
                $this->addError('person', '联系人不能为空');
            }
        }    
    }

     //保存前处理
    public function beforeSave($insert) {//{{{

        if($insert){
            $this->created_at = date("Y-m-d H:i:s");
            $this->user_id    = Yii::$app->user->id;
            $this->status = 1;
        }
        return parent::beforeSave($insert);

    }//}}}



    //获取酒店品牌
    public function getBrand()
    {
        if($this->type == 1){
            $brand = HotelBrand::findOne($this->hotel_group);
            if($brand){
                return $brand->en_name.'——'.$brand->zh_name;
            }
        }
        return ' ';
    }

    //获取英文酒店品牌
    public function getEnBrand()
    {
        if($this->type == 1){
            $brand = HotelBrand::findOne($this->hotel_group);
            if($brand){
                return $brand->en_name;
            }
        }
        return ' ';
    }

    //获取城市名称
    public function getCityName(){
        $model = CityCode::find()->where(['code'=>$this->city])->one();
        return $model;
    }

    //获取录入人
    public function getUser(){
        return $this->hasOne(User::className(), ['id' => 'user_id']);
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'type' => Yii::t('app.supplier', 'Type'),
            'en_name' => Yii::t('app.supplier', 'En Name'),
            'hotel_group' => Yii::t('app.supplier', 'Hotel Group'),
            'is_tour' => Yii::t('app.supplier', 'Is Tour'),
            'person' => Yii::t('app.supplier', 'Person'),
            'phone' => Yii::t('app.supplier', 'Phone'),
            'address' => Yii::t('app.supplier', 'Address'),
            'city' => Yii::t('app.supplier', 'City'),
            'email' => Yii::t('app.supplier', 'Email'),
            'url' => Yii::t('app.supplier', 'Url'),
            'remark' => Yii::t('app.supplier', 'Remark'),
            'status' => Yii::t('app', 'Status'),
            'user_id' => Yii::t('app', 'User ID'),
            'created_at' => Yii::t('app', 'Created At'),
            'updated_at' => Yii::t('app', 'Updated At'),
        ];
    }
}
